package com.example.test.controller;

import com.example.test.Request.ItemRequest;
import com.example.test.entity.Cart;
import com.example.test.entity.Product;
import com.example.test.mapper.CartMapper;
import com.example.test.service.CartItemMatchingService;
import com.example.test.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Service
@RestController
@CrossOrigin
public class Cartcontroller {

    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private CartItemMatchingService cartItemMatchingService;
    @Autowired
    private CartService cartService;


    @PutMapping("/cart")
    public void AdditemToCart( @RequestBody ItemRequest ieq) {
        Cart cart = new Cart();
        cart.setItemId(ieq.getId());
        cartMapper.insert(cart);
    }
    @GetMapping("/cartitem")
    public List<Product>getMatchedItems(){
        return cartItemMatchingService.matchCartItems();
    }
    @PostMapping("/deleteCart")
    public ResponseEntity<Void> deleteCart(){
      cartService.deleteCart();
      return ResponseEntity.noContent().build();
    }
    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") int itemId){
        cartService.delete(itemId);
    }
}
